package cn.itcast.usermanage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@RequestMapping("user")
@Controller
public class UserController {
	@Autowired
	private UserService userService;
	@RequestMapping(value="users")
	public String toUsers(){
		return "users";
	}
	@RequestMapping("list")
	@ResponseBody
	public EasyUIResult queryUsersByPage(@RequestParam("page")Integer pageNum,@RequestParam("rows")Integer pageSize){
		return this.userService.queryEasyUIResult(pageNum,pageSize);
	}
	@RequestMapping(value="/page/{pageName}")
	public String toUserAdd(@PathVariable("pageName")String pageName){
		return pageName;
	}
	/**
	 * 请求路径：/save
	 * 方法返回值：{status：200}
	 * 参数：User对象
	 */
	/*@RequestMapping(value="save")
	@ResponseBody
	public Map<String, Object> addUser(User user){
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			// 调用userService的新增方法
			boolean b = this.userService.addUser(user);
			if(b){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			map.put("status", "500");
			e.printStackTrace();
		}
		return map;
	}*/
	/**
	 * 新增用户数据
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<Void> saveUser(User user){
		try {
			if(user==null || StringUtils.isEmpty(user)){
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			this.userService.saveUser(user);
//			//201
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	/*@RequestMapping(value="delete")
	@ResponseBody
	public Map<String, Object> deleteUser(@RequestParam("ids")List<Object> ids){
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			// 调用userService的新增方法
			boolean b = this.userService.deleteUser(ids);
			if(b){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			map.put("status", "500");
			e.printStackTrace();
		}
		return map;
	}*/
	/**
	 * 删除用户数据
	 */
	@RequestMapping(method=RequestMethod.DELETE)
	public ResponseEntity<Void> deleteUser(@RequestParam("ids") List<Object> ids){
		try {
			if(ids==null){
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			boolean b = this.userService.deleteUser(ids);
			if(b){
				//204
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}else{
				//404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	/**
	 * excel导出分页数据
	 * @param model
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/export/excel")
	public String exportExcel(Model model,@RequestParam("page")Integer pageNum,@RequestParam("rows")Integer pageSize){
		//查询分页数据
		EasyUIResult result = this.userService.queryEasyUIResult(pageNum, pageSize);
		//添加数据模型
		model.addAttribute("userList", result.getRows());
		//返回视图名称
		return "excelView";
	}
}
